package com.qd.common.panda.domain.enums;

import com.cdqidi.exception.ApiException;
import org.springframework.util.StringUtils;

import java.util.Arrays;

/**
 * 停车场计费规则
 * bill_rule1	按小时计费
 * bill_rule2	有起价按小时计费
 * bill_rule3	有起价按小时有封顶计费
 * bill_rule4	分时段按小时计费
 * bill_rule5	分时段有起价按小时计费
 * bill_rule6	分时段混合计费
 *
 * @author sjk
 */
public enum CarParkBillRuleEnum {
    /**
     * 按小时
     */
    BILL_RULE1("bill_rule1", "按小时计费"),
    /**
     * 免费
     */
    BILL_RULE0("bill_rule0", "不计费"),
    /**
     * 起价按小时
     */
    BILL_RULE2("bill_rule2", "有起价按小时计费"),
    /**
     * 起价按小时有封顶
     */
    BILL_RULE3("bill_rule3", "有起价按小时有封顶计费"),
    /**
     * 分时段按小时
     */
    BILL_RULE4("bill_rule4", "分时段按小时计费"),
    /**
     * 分时段有起价按小时
     */
    BILL_RULE5("bill_rule5", "分时段有起价按小时计费"),
    /**
     * 分时段混合
     */
    BILL_RULE6("bill_rule6", "分时段混合计费");

    final String code;
    final String msg;

    CarParkBillRuleEnum(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public static void checkCode(String code) {
        if (!StringUtils.hasLength(code)) {
            throw new ApiException("计费规则不能为空");
        }
        if (Arrays.stream(CarParkBillRuleEnum.values()).filter(carParkBillRule -> carParkBillRule.getCode().equals(code)).findFirst().isEmpty()) {
            throw new ApiException("计费规则ID不存在");
        }
    }

    /**
     * 提前判断，用于解决
     * Case中出现的Constant expression required
     *
     * @param code 编码
     * @return 对应的枚举类
     */
    public static CarParkBillRuleEnum getByValue(String code) {
        for (CarParkBillRuleEnum carParkBillRuleEnum : values()) {
            if (carParkBillRuleEnum.getCode().equals(code)) {
                return carParkBillRuleEnum;
            }
        }
        return null;
    }
}
